<template>
    <div>
        <h3>科室添加</h3>
        <table class="table table-bordered">
            <tbody>
                <tr>
                    <td>科室名称</td>
                    <td><input type="text" v-model="arr.keShiName" /></td>
                </tr>
                <tr>
                    <td>科室照片</td>
                    <td>
                        <input type="file" @change="Page" />
                        <img :src="arr.keShiImg" width="100" height="100" v-if="arr.keShiImg != ''" />
                    </td>
                </tr>
                <tr>
                    <td>成立日期</td>
                    <td><input type="date" v-model="arr.keShiTime" /></td>
                </tr>
                <tr>
                    <td></td>
                    <td><input type="button" value="添加" @click="Ok" class="btn btn-info" /></td>
                </tr>
            </tbody>
        </table>
    </div>
</template>

<script setup lang="ts">
import { reactive, ref, onMounted } from 'vue';
import axios from 'axios';
import { useRoute, useRouter } from 'vue-router';

let router = useRouter();

let arr: any = ref({
    "keShiName": "",
    "keShiImg": "",
    "keShiTime": ""
})


const Ok = () => {
    if (arr.value.keShiName == "") {
        alert('科室名称不能为空');
        return;
    }
    if (arr.value.keShiImg == "") {
        alert('科室照片不能为空');
        return;
    }
    if (arr.value.keShiTime == "") {
        alert('成立日期不能为空');
        return;
    }

    axios({
        url: "/api/KeShi/KeShiAdd",
        method: "post",
        data: arr.value
    })
        .then(res => {
            if (res.data > 0) {
                alert('添加成功');
                router.push('/KeShiShow');

            }
            else if (res.data == -1) {
                alert('名称重复');
                return;
            }
            else {
                alert('添加失败');
            }
        })
        .catch(err => {

        })


}


const Page = (e: any) => {
    let f = e.target.files[0];
    let fs = new FormData();
    fs.append('file', f);

    axios({
        url: "/api/Img/Img",
        method: "post",
        data: fs
    })
        .then(res => {
            if (res.data == "格式不对") {
                alert('格式不对');
                return
            }
            if (res.data == "图片太大") {
                alert('图片大小不能超过2M');
                return
            }
            arr.value.keShiImg = res.data;
        })
}

</script>

<style scoped></style>